/**
 * COPYRIGHT HangZhou Trustway Technology Company Limited
 * All right reserved.
 */
package com.robam.cloud.authcenter.service.impl;

import com.robam.cloud.authcenter.dao.AuthPlatformMapper;
import com.robam.cloud.authcenter.helper.ThirdAuthGateway;
import com.robam.cloud.authcenter.manager.ThirdAuthManager;
import com.robam.cloud.authcenter.service.MediaService;
import com.robam.cloud.common.bean.authcenter.entity.AuthPlatform;
import com.robam.cloud.common.bean.authcenter.enumspace.AuthPlatformTypeEnum;
import com.robam.cloud.common.bean.restful.entity.ServiceException;
import com.robam.cloud.common.bean.usercenter.entity.LoginAccount;
import com.robam.cloud.restful.config.initdata.CurrentAccountAndUserData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description
 * @Author 薛铁琪
 * @CreateTime 2020-08-19 16:48
 * @Version 1.0
 */
@Service
public class MediaServiceImpl implements MediaService {
    @Autowired
    CurrentAccountAndUserData currentAccountAndUserData;
    @Autowired
    AuthPlatformMapper authPlatformMapper;
    @Autowired
    ThirdAuthManager thirdAuthManager;

    @Override
    public String getMediaUrl(String serverId, HttpServletRequest httpServletRequest) {
        LoginAccount loginAccount = currentAccountAndUserData.getCurrentLoginAccount(httpServletRequest);
        AuthPlatform authPlatform = this.authPlatformMapper.selectByPrimaryKey(loginAccount.getPlatformId());
        AuthPlatformTypeEnum authPlatformTypeEnum = AuthPlatformTypeEnum.getAuthPlatformTypeEnumByCode(authPlatform.getPaltformType());
        if (authPlatform == null) {
            throw new ServiceException("登录平台uuid不正确请检查后重试");
        }
        ThirdAuthGateway thirdAuthGateway = thirdAuthManager.call(authPlatformTypeEnum);
        return thirdAuthGateway.getMediaUrl(authPlatform, serverId);
    }
}
